#include "customdropeventsubwnd.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDebug>
#include "customheaderview.h"

CustomDropEventSubWnd::CustomDropEventSubWnd(QWidget *parent) : QListWidget(parent)
{
    this->setAcceptDrops(true);
    this->setFlow(Flow::LeftToRight);
    this->setDragEnabled(true);
    this->setDefaultDropAction(Qt::DropAction::CopyAction);
}

void CustomDropEventSubWnd::removeSelectItem()
{
    QListWidgetItem* _curItem=this->currentItem();
    if(_curItem==nullptr)return;
    int _pos=this->row(_curItem);
    this->takeItem(_pos);
    update();
}

void CustomDropEventSubWnd::dragEnterEvent(QDragEnterEvent *event)
{
    event->accept();
}

void CustomDropEventSubWnd::dragMoveEvent(QDragMoveEvent *event)
{

}

void CustomDropEventSubWnd::dropEvent(QDropEvent *event)
{
    const QMimeData* _md=event->mimeData();
    if(!_md->hasFormat("table/content"))return;
    CustomHeaderView* _tableview=static_cast<CustomHeaderView*>(event->source());
    if(_tableview)
    {
        _tableview->removeCurColumn();
    }
    QByteArray _ba=_md->data("table/content");
    QString _content(_ba);
    QListWidgetItem* _item=new QListWidgetItem;
    _item->setText(_content);
    QPoint _pos=event->pos();
    QListWidgetItem* _hoverItem= this->itemAt(_pos);
    int _insertPos=-1;
    if(_hoverItem==nullptr)
    {
        _insertPos=this->count();
    }
    else
    {
        _insertPos=this->row(_hoverItem);
    }
    this->insertItem(_insertPos,_item);
}

